package cn.kshost.zqyg.manager.service.impl;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import cn.kshost.zqyg.manager.service.ValidateCodeService;
import cn.kshost.zqyg.model.vo.system.ValidateCodeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class ValidateCodeServiceImpl implements ValidateCodeService {
@Autowired
private RedisTemplate<String,String> redisTemplate;
    @Override
    public ValidateCodeVo generateValidateCode() {
        CircleCaptcha circleCaptcha = CaptchaUtil.createCircleCaptcha(190, 60, 4, 20);
        //获取验证码的值
        String code = circleCaptcha.getCode();
        //获取验证码图像
        String imageBase64 = circleCaptcha.getImageBase64();

        //生成验证码的key
        String key =UUID.randomUUID().toString().replace("-", "");

        //将验证码的值存入redis
        redisTemplate.opsForValue().set("user:login:validatecode:"+key,code);

        ValidateCodeVo validateCodeVo = new ValidateCodeVo();
        validateCodeVo.setCodeKey(key);
        validateCodeVo.setCodeValue("data:image/png;base64,"+imageBase64);

        return validateCodeVo;


    }
}
